const Controller = require('egg').Controller;

class FollowController extends Controller {
    // 关注用户
    async follow() {
        const { ctx, service } = this;

        try {
            // 参数验证
            const rules = {
                follow_id: { type: 'string', required: true }
            };
            ctx.validate(rules);

            const { follow_id } = ctx.request.body;

            // 从token中获取用户ID
            const token = ctx.request.header.token;
            const decode = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

            // 不能关注自己
            if (parseInt(decode.user_id) === parseInt(follow_id)) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: '不能关注自己'
                };
                return;
            }

            const result = await service.follow.followUser(decode.user_id, follow_id);

            ctx.body = {
                code: 0,
                data: result,
                msg: '关注成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '关注失败'
            };
        }
    }

    // 取消关注
    async unfollow() {
        const { ctx, service } = this;

        try {
            const { follow_id } = ctx.params;

            // 验证参数
            const rules = {
                follow_id: { type: 'string', required: true }
            };
            ctx.validate(rules, ctx.params);

            // 从token中获取用户ID
            const token = ctx.request.header.token;
            const decode = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

            await service.follow.unfollowUser(decode.user_id, follow_id);

            ctx.body = {
                code: 0,
                data: null,
                msg: '取消关注成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '取消关注失败'
            };
        }
    }

    // 获取关注列表
    async following() {
        const { ctx, service } = this;

        try {
            const { user_id } = ctx.params;
            const query = ctx.query;

            // 验证参数
            const rules = {
                user_id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            const result = await service.follow.getFollowingList(parseInt(user_id), query);

            ctx.body = {
                code: 0,
                data: result,
                msg: '获取成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取关注列表失败'
            };
        }
    }

    // 获取粉丝列表
    async followers() {
        const { ctx, service } = this;

        try {
            const { user_id } = ctx.params;
            const query = ctx.query;

            // 验证参数
            const rules = {
                user_id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            const result = await service.follow.getFollowersList(parseInt(user_id), query);

            ctx.body = {
                code: 0,
                data: result,
                msg: '获取成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '获取粉丝列表失败'
            };
        }
    }

    // 检查是否关注
    async check() {
        const { ctx, service } = this;

        try {
            const { user_id } = ctx.params;

            // 验证参数
            const rules = {
                user_id: { type: 'id', required: true }
            };
            ctx.validate(rules, ctx.params);

            // 从token中获取用户ID
            const token = ctx.request.header.token;
            const decode = ctx.app.jwt.verify(token, ctx.app.config.jwt.secret);

            const isFollowing = await service.follow.checkFollowing(decode.user_id, parseInt(user_id));

            ctx.body = {
                code: 0,
                data: { is_following: isFollowing },
                msg: '获取成功'
            };
        } catch (error) {
            if (error.errors) {
                ctx.body = {
                    code: 1,
                    data: null,
                    msg: `参数错误: ${error.errors[0].field} ${error.errors[0].message}`
                };
                return;
            }

            ctx.body = {
                code: 1,
                data: null,
                msg: error.message || '检查关注状态失败'
            };
        }
    }
}

module.exports = FollowController;